package study.sharding.shardingjdbc.support.hint;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class MasterAspect {

    @Around("@annotation(master)")
    public Object around(ProceedingJoinPoint joinPoint, Master master) throws Throwable {
        //日志
        Signature signature = joinPoint.getSignature();
        log.info(signature.getName());
        log.info(signature.getDeclaringTypeName());
        log.info(signature.getDeclaringType().getSimpleName());
        //
        String longName = signature.toLongString();
        String name = signature.toString();
        String shortName = signature.toShortString();
        log.info(longName);
        log.info(name);
        log.info(shortName);

        //
        Object result;
        try {
            log.info(">>>>>> Set Master Route! [{}] ", shortName);
            HintUtils.setMasterRouteOnly();
            log.info("111");
            result = joinPoint.proceed();
            log.info("222");
        } catch (Throwable ex) {
            throw ex;
        } finally {
            log.info(">>>>>> Clear Master Route! [{}]", shortName);
            HintUtils.clear();
        }
        return result;
    }
}
